package com.sion.difytest;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements LLMApi.OnAnswerReceivedListener {
    private EditText editText;
    private Button btn_send;
    private RecyclerView recyclerView;
    private MyAdapter adapter;
    private List<Message> data = new ArrayList<>();
    private LLMWorkflowApi llmWorkflowApi;
    private Message currentReceivingMessage;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        bindView();
        init_data();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

        adapter = new MyAdapter(data, this);
        recyclerView.setAdapter(adapter);

        // 初始化 LLMWorkflowApi
        llmWorkflowApi = new LLMWorkflowApi();
        llmWorkflowApi.setReceivedListener(this);

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = editText.getText().toString();
                if (!s.equals("")) {
                    Message message = new Message(s, Message.TYPE_SEND);
                    data.add(message);
                    // 当有新消息时，刷新 RecyclerView
                    adapter.notifyItemInserted(data.size() - 1);
                    // 将屏幕滚动到最新消息
                    recyclerView.scrollToPosition(data.size() - 10);
                    editText.setText("");

                    // 重置当前接收消息
                    currentReceivingMessage = null;

                    // 调用 LLMWorkflowApi 发送请求
                    llmWorkflowApi.sendMessage(s);
                } else {
                    Toast.makeText(MainActivity.this, "发送消息不能为空！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void bindView() {
        editText = findViewById(R.id.edit);
        btn_send = findViewById(R.id.btn_send);
        recyclerView = findViewById(R.id.rv);
    }

    public void init_data() {
        Message meg1 = new Message("Hello, 我叫白文磊", Message.TYPE_RECEIVE);
        data.add(meg1);

        Message meg2 = new Message("Hello, 我叫郭涵博", Message.TYPE_SEND);
        data.add(meg2);
    }

    @Override
    public void onAnswerReceived(String result) {
        runOnUiThread(() -> {
            if (currentReceivingMessage == null) {
                currentReceivingMessage = new Message("", Message.TYPE_RECEIVE);
                data.add(currentReceivingMessage);
                adapter.notifyItemInserted(data.size() - 1);
                recyclerView.scrollToPosition(data.size() - 1);
            }
            updateMessageGradually(currentReceivingMessage, result);
        });
    }

    private void updateMessageGradually(Message message, String newText) {
        String currentContent = message.getContent();
        String newContent = currentContent + newText;
        message.setContent(newContent);
        adapter.notifyItemChanged(data.indexOf(message));
        recyclerView.scrollToPosition(data.size() - 1);
    }

    @Override
    public void onFailure(String msg) {
        runOnUiThread(() -> {
            // 处理请求失败的情况
            Toast.makeText(this, "请求失败：" + msg, Toast.LENGTH_SHORT).show();
        });
    }
}